home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
DIDIRRD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-13
|
5KB
|
152 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: didirrd.c
// Title: Data File I/O Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to read a directory entry.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <di.h>
//----------------------------------------------------------------------------
// Description: Read directory entry
// Parameters: hpf Physical file handle
// cDir Directory entry to read
// pdir Buffer to receive directory entry
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioDirRead(HPF hpf, SIZET cDir, PDATADIR pdir)
{
DATAHDR hdr;
FPOS fpos;
SIZET i;
// Validate file handle
Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
Assert(di.physical[hpf].fUsed);
if (di.physical[hpf].fl & PF_ERROR)
return FALSE;
if (!DioHeaderRead(hpf, &hdr)) // Read header information
return FALSE;
Assert(pdir);
Assert(cDir < hdr.usDirectoryEntries);
//
// If physical file directory is not cached, read it in.
//
if (di.physical[hpf].pdatadir == NULL)
{
SIZET cBuf = (SIZET)hdr.usDirectoryEntries * sizeof(DATADIR);
SIZET cEntry = sizeof(DATADIR) - sizeof(ULONG);
// Allocate a buffer
di.physical[hpf].pdatadir = (PDATADIR)MemAlloc(cBuf);
if (di.physical[hpf].pdatadir == NULL)
{
ErrorNoMem();
goto ERROR_EXIT;
}
fpos = (LONG)sizeof(DATAHDR);
if (!FileRead(di.physical[hpf].hf, di.physical[hpf].pdatadir, cBuf, fpos))
goto ERROR_EXIT;
// Validate CRC codes for entries
for (i = 0; i < (SIZET)hdr.usDirectoryEntries; ++i)
if (di.physical[hpf].pdatadir[i].crc
!= (ULONG)CrcCalc((PBYTE)&di.physical[hpf].pdatadir[i], cEntry))
{
Error("Data file is corrupt.\nDirectory entry CRC code does not verify.");
goto ERROR_EXIT;
}
} // Return directory entry contents
*pdir = di.physical[hpf].pdatadir[cDir];
if (di.physical[hpf].fWriteable // If file is writeable, destroy cache
&& di.physical[hpf].pdatadir)
{
MemFree(di.physical[hpf].pdatadir);
di.physical[hpf].pdatadir = NULL;
}
return TRUE;
ERROR_EXIT:
if (di.physical[hpf].pdatadir)
{
MemFree(di.physical[hpf].pdatadir);
di.physical[hpf].pdatadir = NULL;
}
di.physical[hpf].fl |= PF_ERROR;
return FALSE;
}
//----------------------------------------------------------------------------
// Description: Release directory cache
// Parameters: hpf Physical file handle
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioDirRelease(HPF hpf)
{
Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
Assert(di.physical[hpf].fUsed);
if (di.physical[hpf].pdatadir)
{
MemFree(di.physical[hpf].pdatadir);
di.physical[hpf].pdatadir = NULL;
}
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Release directory cache
// Parameters: hpf Physical file handle
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioDirReleaseAll(void)
{
BOOL fResult = TRUE;
HPF hpf;
for (hpf = 0; hpf < MAX_PHYSICAL_FILES; ++hpf)
if (di.physical[hpf].fUsed && !DioDirRelease(hpf))
fResult = FALSE;
return fResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------